package com.ujiuye.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ujiuye.pojo.Project;
import com.ujiuye.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @RequestMapping("/list")
    public PageInfo<Project> list(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum
    ){
        try {
            PageHelper.startPage(pageNum,6);
            List<Project> list = projectService.list();
            PageInfo<Project> pageInfo = new PageInfo<>(list);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/get")
    public Project get(int id){
        try {
            return projectService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/save")
    public String save (Project project){
        try {
            projectService.save(project);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @RequestMapping("/update")
    public String update (Project project){
        try {
            projectService.update(project);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    @RequestMapping("/remove")
    public String remove (int id){
        try {
            projectService.remove(id);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }
}
